package org.jsbd.boss.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    public static String zipFile(String data, String fileName, String filePath) throws Exception {
        File file = new File(filePath);
        if(!file.exists()){
            file.mkdir();
            file.setWritable(true);
        }
        String targeFileName = filePath + "/" +System.currentTimeMillis() + RandomUtils.getRandomNum(10000);
        File file1 = new File(targeFileName);
        if(!file1.exists()){
            file1.createNewFile();
            file1.setWritable(true);
        }
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(targeFileName));
        BufferedOutputStream bos = new BufferedOutputStream(zos);
        zos.putNextEntry(new ZipEntry(fileName));
        bos.write(data.getBytes());
        bos.flush();
        zos.close();
        return targeFileName;
    }


    public static byte[] zipFile(String dataStr,String name) throws Exception {
        byte[] data=dataStr.getBytes();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(bos);
        ZipEntry entry = new ZipEntry(name);
        entry.setSize(data.length);
        zip.putNextEntry(entry);
        zip.write(data);
        zip.closeEntry();
        zip.close();
        bos.close();
        return bos.toByteArray();
    }

    public static byte[] unZip(String filePath) throws Exception {
        FileInputStream fis = new FileInputStream(filePath);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int c = 0;
        byte[] buf = new byte[1024];
        while ((c = fis.read(buf)) != -1) {
            bos.write(buf, 0, c);
        }
        return bos.toByteArray();
    }



    public static List<byte[]> unZipFolder(InputStream fileInputStream) throws Exception {

        ZipInputStream inZip = new ZipInputStream(fileInputStream);
        ZipEntry zipEntry;
        List<byte[]> fileList = new ArrayList<>();
        while ((zipEntry = inZip.getNextEntry()) != null) {
            if (!zipEntry.isDirectory()) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                int len;
                byte[] buffer = new byte[1024];
                while ((len = inZip.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                    bos.flush();
                }
                bos.close();
                fileList.add(bos.toByteArray());
            }
        }
        inZip.close();
        return fileList;
    }


    public static byte[] readFileContent(String filePath) throws Exception {
        FileInputStream fis = new FileInputStream(filePath);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int c = 0;
        byte[] buf = new byte[1024];
        while ((c = fis.read(buf)) != -1) {
            bos.write(buf, 0, c);
        }
        return bos.toByteArray();
    }


}